<?

# Include database classfile
include("class.poll.database.php");

# Class new_poll
class new_poll
{
    function showAdmin()
    {
        $admin .= "<form method=\"POST\" ation=\"" . $_SERVER['PHP_SELF'] . "\">";
        $admin .= "Question :<br />";
        $admin .= "<input type=\"text\" name=\"q\" maxlength=\"255\" /><br />";
        $admin .= "Answers : (separated by a comma with a space)<br />";
        $admin .= "<input type=\"text\" name=\"a\" /><br />";
        $admin .= "<input type=\"submit\" name=\"add\" value=\"Add poll!\" />";
        $admin .= "</form>";

        return $admin;
    }

    function makeScores($num)
    {
        for($i=1;$i<=$num;$i++)
        {
            if($i == $num)
            {
                $scores .= "0";
            }
            else
            {
                $scores .= "0,";
            }
        }
        return $scores;
    }
}

# Check if user is Admin
$admin = 1;
if($admin)
{
    # Show options
    $np = new new_poll;
    $db = new database;

    # Check if form is submitted
    if(!$_POST['add'])
    {
        # Show admin
        echo $np->showAdmin();
    }
    else
    {
        # Set some vars
        $a = $_POST['a'];
        $total = count(explode(",", $a));
        $q = htmlentities($_POST['q']);
        $a = $_POST['a'];
        # calc null values
        $null = $np->makeScores($total);

        # Insert new poll
        $sql1 = "INSERT INTO `poll` SET `poll_question` = '" . $q . "', `poll_options` = '" . $a . "', `poll_score` = '" . $null . "', `poll_date` = '" . time() . "'";
        # Reset votes
        $sql2 = "DELETE FROM ".$prefix."`votes`";

        $res1 = $db->execute($sql1);
        $res2 = $db->execute($sql2);

        echo "New poll added!";
    }
}
else
{
    # User isnt an admin
    die("INVALID ACCESLEVEL!");
}

?> 